//
//  TTNetworkingTools.m
//  OC网络工具
//
//  Created by 陶宏路 on 16/7/20.
//  Copyright © 2016年 陶路路. All rights reserved.
//

#import "TTNetworkingTools.h"
#import "TTWelconmeModel.h"
@implementation TTNetworkingTools

// 单例

+(instancetype)sharedManager{
    
    static TTNetworkingTools * instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        
        instance = [[TTNetworkingTools alloc]initWithSessionConfiguration:configuration];
      
        instance.requestSerializer.timeoutInterval = 10;
        
//        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain" ,nil];
        
    });
    
    return instance;
}

-(void)request:(TTNetworkingType)type urlString:(NSString*)urlString parameters:(NSDictionary*)parameters sucessBlock:(void(^)(id responseObject))sucessBlock error:(void(^)(NSError*error))errorBlock{
    
    if (type == GET) {
       
        [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            sucessBlock(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            errorBlock(error);
        }];
    }
    
    if (type == POST) {
        
       [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           
           sucessBlock(responseObject);
           
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           
           errorBlock(error);
       }];
    }
}

-(void)loadWelcomeDatasucessBlock:(NSString*)code sucessBlock:(void(^)(id responseObject))sucessBlock error:(void(^)(NSError*error))errorBlock{
    
    /*
     urlString:String = "https://api.weibo.com/oauth2/access_token"
     let parameters = ["client_id":AppKey,"client_secret":AppSecret,"grant_type":"authorization_code","code":code,"redirect_uri":APPREDIRECT_URI]
    

     */
    
    NSString * urlString = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary * parameters = @{@"client_id":AppKey,@"client_secret": AppSecret,@"grant_type":@"authorization_code",@"code":code,@"redirect_uri":APPREDIRECT_URI};
    
    [self request:POST urlString:urlString parameters:parameters sucessBlock:sucessBlock error:errorBlock];
}


-(void)loadUserInfo:(TTWelconmeModel*)model sucessBlock:(void(^)(id responseObject))sucessBlock error:(void(^)(NSError*error))errorBlock{
    
    NSDictionary * parameters =@{@"access_token":model.access_token,@"uid":model.uid};
    
    [self request:GET urlString:urlStringT parameters:parameters sucessBlock:sucessBlock error:errorBlock];
    
}

-(void)loadHomeData:(TTWelconmeModel*)model since_id:(int64_t)since_id max_id:(int64_t)max_id sucessBlock:(void(^)(id responseObject))sucessBlock error:(void(^)(NSError*error))errorBlock{
    
    NSString * homeUrlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    
//    since_id	false	int_64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
//    max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
    
    NSDictionary * parameters = @{@"access_token":model.access_token,@"since_id":[NSString stringWithFormat:@"%lld",since_id],@"max_id":[NSString stringWithFormat:@"%lld",max_id]};
    
    [self request:GET urlString:homeUrlString parameters:parameters sucessBlock:sucessBlock error:errorBlock];
    
    
}

@end
